import java.io.*;

public class demo2 {
    public static void main(String[] args) throws InterruptedException, IOException {
        File file = new File("../test.txt");
        Writer writer = new FileWriter(file,true);
        Reader reader = new FileReader(file);

        Object locker = new Object();
        Thread w = new Thread(() ->{
            synchronized (locker) {
                try {
                    for(char c = 'a';c<='z';c++){
                        writer.write(c);
                    }
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread r = new Thread(() ->{
            synchronized (locker) {
                try {
//                    Thread.sleep(0);
                    while (true){
                        int n = reader.read();
                        if(n== -1){
                            break;
                        }
                        System.out.print((char)n+" ");
                    }
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        w.start();
        r.start();
        w.join();
        r.join();

    }
}
